<?php

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
    if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
        case "text":
	        $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	        break;    
        case "long":
    	case "int":
	        $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	        break;
	    case "double":
	        $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
	        break;
	    case "date":
	        $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	        break;
	    case "defined":
	        $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
	        break;
    }
    return $theValue;
}

function getNewsType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_ON_NEWS;
			break;
		case 2:
			$text = TEXT_INFO_NEWS;
			break;
	}
	return $text;
}

function getPetType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_PET_DOG;
			break;
		case 2:
			$text = TEXT_PET_CAT;
			break;
		case 3:
			$text = TEXT_PET_OTHER;
			break;
		case 4:
			$text = TEXT_PET_BOTH;
			break;
	}
	return $text;
}

function getGender($number) {
	switch ($number) {
		case 1:
			$text = TEXT_PET_BOY;
			break;
		case 2:
			$text = TEXT_PET_GIRL;
			break;
	}
	return $text;
}

function getOrigin($number) {
	switch ($number) {
		case 1:
			$text = TEXT_PET_HOMELESS;
			break;
		case 2:
			$text = TEXT_PET_HOMEBRED;
			break;
	}
	return $text;
}

function getAgeGrp($number) {
	switch ($number) {
		case 1:
			$text = TEXT_PET_AGE_GRP_1;
			break;
		case 2:
			$text = TEXT_PET_AGE_GRP_2;
			break;
	}
	return $text;
}

function getSpay($number) {
	switch ($number) {
		case 'Y':
			$text = TEXT_PET_SPAYED;
			break;
		case 'N':
			$text = TEXT_PET_NOT_SPAYED;
			break;
		case 3:
			$text = TEXT_PET_UNKNOWN;
			break;
	}
	return $text;
}

function getVaccinate($number) {
	switch ($number) {
		case 'Y':
			$text = TEXT_PET_VACCINATED;
			break;
		case 'N':
			$text = TEXT_PET_NOT_VACCINATED;
			break;
		case 3:
			$text = TEXT_PET_UNKNOWN;
			break;
	}
	return $text;
}

function getDewormin($number) {
	switch ($number) {
		case 'Y':
			$text = TEXT_PET_DEWORMIN;
			break;
		case 'N':
			$text = TEXT_PET_NOT_DEWORMIN;
			break;
		case 3:
			$text = TEXT_PET_UNKNOWN;
			break;
	}
	return $text;
}

function getDewormext($number) {
	switch ($number) {
		case 'Y':
			$text = TEXT_PET_DEWORMEXT;
			break;
		case 'N':
			$text = TEXT_PET_NOT_DEWORMEXT;
			break;
		case 3:
			$text = TEXT_PET_UNKNOWN;
			break;
	}
	return $text;
}

function getDoctorCheck($number) {
	switch ($number) {
		case 'N':
			$text = TEXT_PET_NOT_CHECKED;
			break;
		case 3:
			$text = TEXT_PET_UNKNOWN;
			break;
		default:
			$text = TEXT_PET_CHECKED;
			break;
	}
	return $text;
}

function getIsAdopt($number) {
	switch ($number) {
		case 1:
			$text = TEXT_NOT_ADOPTED;
			break;
		case 2:
			$text = TEXT_ADOPTED;
			break;
		case 3:
			$text = TEXT_RETURNED;
			break;
		case 4:
			$text = TEXT_RESCUE_ADOPTED;
			break;
		case 5:
			$text = TEXT_DIE;
			break;
		case 6:
			$text = TEXT_OTHER;
			break;
	}
	return $text;
}

function getLostType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_LOST;
			break;
		case 2:
			$text = TEXT_FOUND;
			break;
	}
	return $text;
}

function getPetSick($number) {
	switch ($number) {
		case 1:
			$text = TEXT_INFO_ISSUE;
			break;
		case 2:
			$text = TEXT_INFO_CAT;
			break;
		case 3:
			$text = TEXT_INFO_DOG;
			break;
	}
	return $text;
}

function getIfPartner($char) {
	switch ($char) {
		case 'Y':
			$text = TEXT_PARTNER;
			break;
		case 'N':
			$text = TEXT_NOT_PARTNER;
			break;
	}
	return $text;
}

function getOpusType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_INFO_BOOK;
			break;
		case 3:
			$text = TEXT_INFO_STU;
			break;
		default :
			$text = TEXT_INFO_MOVIE;
			break;
	}
	return $text;
}

function getMemberSex($number) {
	switch ($number) {
		case MEMBER_MALE:
			$text = TEXT_MEMBER_MALE;
			break;
		case MEMBER_FEMALE:
			$text = TEXT_MEMBER_FEMALE;
			break;
	}
	return $text;
}

function getMemberStatus($number) {
	switch ($number) {
		case 1:
			$text = TEXT_MEMBER_STATUS_1;
			break;
		case 2:
			$text = TEXT_MEMBER_STATUS_2;
			break;
		case 3:
			$text = TEXT_MEMBER_STATUS_3;
			break;
		case 4:
			$text = TEXT_MEMBER_STATUS_4;
			break;
		case 5:
			$text = TEXT_MEMBER_STATUS_5;
			break;
	}
	return $text;
}

function getMemberType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_MEMBER_TYPE_1;
			break;
		case 2:
			$text = TEXT_MEMBER_TYPE_2;
			break;
		case 4:
			$text = TEXT_MEMBER_TYPE_4;
			break;
		case 5:
			$text = TEXT_MEMBER_TYPE_5;
			break;
	}
	return $text;
}

function getIfPaid($number) {
	switch ($number) {
		case 1:
			$text = TEXT_WILLING;
			break;
		case 2:
			$text = TEXT_NOT_WILLING;
			break;
	}
	return $text;
}

function getIfVolunteer($number) {
	switch ($number) {
		case 1:
			$text = TEXT_WILLING;
			break;
		case 2:
			$text = TEXT_NOT_WILLING;
			break;
	}
	return $text;
}

function getVolunteerType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_VOLUNTEER_TYPE_1;
			break;
		case 2:
			$text = TEXT_VOLUNTEER_TYPE_2;
			break;
		case 3:
			$text = TEXT_VOLUNTEER_TYPE_3;
			break;
		case 4:
			$text = TEXT_VOLUNTEER_TYPE_4;
			break;
		case 5:
			$text = TEXT_VOLUNTEER_TYPE_5;
			break;
		case 6:
			$text = TEXT_VOLUNTEER_TYPE_6;
			break;
	}
	return $text;
}

function getIfHavePet($char) {
	switch ($char) {
		case 'Y':
			$text = TEXT_YES;
			break;
		case 'N':
			$text = TEXT_NO;
			break;
	}
	return $text;
}

function getIfFreeFoster($num) {
	switch ($num) {
		case 1:
			$text = TEXT_YES;
			break;
		case 2:
			$text = TEXT_NO;
			break;
	}
	return $text;
}

function getHappyHomeType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_SOS_AREA_DOG;
			break;
		case 2:
			$text = TEXT_SOS_HAPPY_CAT;
			break;
	}
	return $text;
}

function getManualType($number) {
	switch ($number) {
		case 1:
			$text = TEXT_INFO_MUSTKNOW;
			break;
		case 2:
			$text = TEXT_INFO_CHILD;
			break;
		case 3:
			$text = TEXT_INFO_MYTHS;
			break;
	}
	return $text;
}

function getAccounceStatus($number) {
	switch ($number) {
		case 1:
			$text = TEXT_ANNOUNCE_DISPLAY;
			break;
		case 0:
			$text = TEXT_ANNOUNCE_NOT_DISPLAY;
			break;
	}
	return $text;
}

function getNewsletterStatus($number) {
	switch ($number) {
		case 1:
			$text = 'Pending';
			break;
		case 0:
			$text = 'Failed';
			break;
		case 2:
			$text = 'Finished';
			break;
	}
	return $text;
}


/*
 * Created on 2009-4-7
 *
 * function for resizing and generating images
 * created by Joe Qiao [mynamesucks@163.com]
 * You can copy this file to wherever you want, but you must remain the author info
 */
function generateImages($trueType, $imageFullName, $configImageWidth, $configImageHeight, $imageName) {
	if($trueType == 'image/jpg' || $trueType == 'image/jpeg') {
		$newImage = imagecreatefromjpeg($imageFullName);
	}
	else if($trueType == 'image/png') {
		$newImage = imagecreatefrompng($imageFullName);
	}
	else if($trueType == 'image/gif') {
		$newImage = imagecreatefromgif($imageFullName);
	}
	$newImageWidth = (int)ImageSX($newImage);
	$newImageHeight = (int)ImageSY($newImage);
	
	//Resize the portrait.
	//If the width is longer than height
	//  If width is longer than config width, then resize width to 70 and height accordingly.
	//  Else if width is equal or shorter than config width, then keep the size.
	//Else if the height is longer than width or they equal
	//  If height is longer than config height, then resize height to 70 and width accordingly.
	//	Else if height is equal or shorter than config height, then keep the size.
	if($newImageWidth > $newImageHeight) {
		if($newImageWidth > $configImageWidth) {
			$destinationWidth = $configImageWidth;
			$destinationHeight = (int)($configImageWidth / $newImageWidth * $newImageHeight);
		}
		else {
			$destinationWidth = $newImageWidth;
			$destinationHeight = $newImageHeight;
		}
	}
	else {
		if($newImageHeight > $configImageHeight) {
			$destinationHeight = $configImageHeight;
			$destinationWidth = (int)($configImageHeight / $newImageHeight * $newImageWidth);
		}
		else {
			$destinationHeight = $newImageHeight;
			$destinationWidth = $newImageWidth;				
		}
	}
	$destinationImage = ImageCreateTrueColor($destinationWidth, $destinationHeight);
	imagecopyresampled($destinationImage, $newImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $newImageWidth, $newImageHeight);
	ImageJpeg($destinationImage, $imageName);
	
	return array($destinationWidth, $destinationHeight);
}

function ValidateAddress($address) {
	if (function_exists('filter_var')) { //Introduced in PHP 5.2
    	if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
        	return false;
    	} else {
        	return true;
    	}
    } 
    else {
    	return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
    }
}

function updateStatus($isSent,$to,$cc,$bcc,$subject,$body,$name=array()) {
    global $db;
    global $insert_id;
    global $batch_name_failed;
    
    $key = key($name);
    echo "&nbsp;- Sending to ".$key."......";
    if(!$isSent /* || $key == 'mynamesucks@163.com'*/) {
    	echo "<font color='red'>failed!!!</font><br>";
    	$sql = "update newsletter_log set failed=failed+1 where id=$insert_id";
    	$db->query($sql);
    	$filename = NEWSLETTER_LOG_MAIL_ADDRESS_PATH.'/'.$batch_name_failed;
    	preg_match("/token=([0-9a-z]*)/", $body, $matches);
    	if(0 < count($matches)) {
    		$link = $matches[1];
    	}
    	else {
    		$link = 'no';
    	}
    	if(is_file($filename)) {
    		$content = file_get_contents($filename);
    		$arr = unserialize($content);
    		array_push($arr, array('name' => $name[$key],
	    						   'email' => $key,
								   'status' => 0,
								   'token' => $link));
			file_put_contents($filename, serialize($arr));
    	}
    	else {
    		$arr = array(array('name' => $name[$key],
	    					   'email' => $key,
							   'status' => 0,
							   'token' => $link));
			file_put_contents($filename, serialize($arr));
    	}
    }
    else {
    	echo "<font color='green'>Succeed!!!</font><br>";
    }
}

?>
